package net.xdclass.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.mapper.LinkGroupMapper;
import net.xdclass.model.GroupCodeMappingDO;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述
 *
 * @author ASUS
 * @date 2024/07/25  20:35
 */
@Component
public class LinkGroupMangerImpl implements LinkGroupManager {
    @Autowired
    private LinkGroupMapper linkGroupMapper;
    @Override
    public int add(LinkGroupDO linkGroupDO) {
        int rows = linkGroupMapper.insert(linkGroupDO);
        return rows;
    }

    @Override
    public boolean del(String groupId, long accountNo) {
        LambdaQueryWrapper<LinkGroupDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(LinkGroupDO::getAccountNo,accountNo).eq(LinkGroupDO::getId,groupId);
        int rows = linkGroupMapper.delete(wrapper);
        return rows==1 ? true : false;

    }

    @Override
    public LinkGroupDO detail(Long groupId, long accountNo) {
        LambdaQueryWrapper<LinkGroupDO> wrapper = new LambdaQueryWrapper<LinkGroupDO>();
        wrapper.eq(LinkGroupDO::getAccountNo,accountNo).eq(LinkGroupDO::getId,groupId);
        LinkGroupDO linkGroupDO = linkGroupMapper.selectOne(wrapper);
//        LinkGroupVO linkGroupVO = new LinkGroupVO();
//        BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
        return linkGroupDO;

    }

    @Override
    public List<LinkGroupVO> listAll(long accountNo) {
        LambdaQueryWrapper<LinkGroupDO> wrapper = new LambdaQueryWrapper<LinkGroupDO>();
        wrapper.eq(LinkGroupDO::getAccountNo,accountNo);
        List<LinkGroupDO> linkGroupDOS = linkGroupMapper.selectList(wrapper);
        List<LinkGroupVO> linkGroupVOS = new ArrayList<>();
        linkGroupDOS.forEach(linkGroupDO -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(linkGroupDO,linkGroupVO);
            linkGroupVOS.add(linkGroupVO);
        });
        return linkGroupVOS;
    }

    @Override
    public int update(LinkGroupUpdateRequest request, long accountNo) {
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(request.getTitle());
        linkGroupDO.setId(request.getId());
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupMapper.updateById(linkGroupDO);
        return rows;
    }


}
